// 引入 gulp
var gulp = require('gulp'),
  jshint = require('gulp-jshint')
  uglify = require('gulp-uglify'),
  sass = require('gulp-sass'),
  less = require('gulp-less'),
  minifyCss = require('gulp-minify-css'),
  imagemin = require('gulp-imagemin'),
  htmlmin = require('gulp-htmlmin'),
  watchPath = require('gulp-watch-path'),
  notify = require("gulp-notify"),
  browserSync = require('browser-sync').create(),
  reload = browserSync.reload;
var handleErrors = function(){
  var args = Array.prototype.slice.call(arguments);
  // console.log(JSON.stringify(arguments));
  notify.onError({
    title: 'compile error',
    message: '<%=error.message %>, at line : <%=error.lineNumber %>'
  }).apply(this, args);//替换为当前对象
  this.emit();//提交
}
// 压缩js文件
gulp.task('script', function() {
    gulp.src('src/js/**/*.js')
        .pipe(jshint())
        .pipe(jshint.reporter('default'))
        .pipe(uglify())
        .on('error', handleErrors)
        .pipe(gulp.dest('dist/js'));
});
// 编译scss或sass文件
// gulp.task('sass', function() {
//     gulp.src('src/scss/**/*')
//         .pipe(minifyCss())
//         .pipe(gulp.dest('dist/css'));
// });

// 编译 less 或 less 文件
gulp.task('less', function() {
    gulp.src('src/less/**/*')
        .pipe(minifyCss())
        .pipe(gulp.dest('dist/css'));
})
// 压缩css文件
gulp.task('css', function() {
    gulp.src('src/css/**/*.css')
        .pipe(minifyCss())
        .pipe(gulp.dest('dist/css'));
});
// 压缩img文件
gulp.task('img', function(){
    gulp.src('src/img/**/*.*')
        .pipe(imagemin())
        .on('error', handleErrors)
        .pipe(gulp.dest('dist/img'));
});
gulp.task('minAll', function(){
    gulp.run('script', 'css', 'img');
});
// 监听js文件的变化，压缩有变化的文件
gulp.task('watchJs', function(){
  var src = 'src/js/**/';
  gulp.watch(src + '*', function(event){
    var paths = watchPath(event , src , 'dist/js/');
    console.log(paths.srcFilename + ' has ' + event.type);
    if(paths.srcFilename.indexOf('.js')>-1){
      gulp.src(src + paths.srcFilename)
          .pipe(jshint())
          .pipe(jshint.reporter('default'))
          .pipe(uglify())
          .on('error', handleErrors)
          .pipe(gulp.dest('dist/js/')).on('change', reload);
    }
  });
});
gulp.task('watchSass', function() {
  var src = 'src/scss/**/';
  gulp.watch(src + '*', function(event){
    var paths = watchPath(event , src , 'dist/css/');
    console.log(paths.srcFilename + ' has ' + event.type);
    if(paths.srcFilename.indexOf('.scss')>-1 || paths.srcFilename.indexOf('.sass')>-1){
      gulp.src('src/scss/**/' + paths.srcFilename)
          .pipe(sass())
          .on('error', handleErrors)
          .pipe(minifyCss())
          .pipe(gulp.dest('dist/css/'))
          .pipe(reload({stream: true}));
    }
  });
});
gulp.task('watchLess', function() {
  var src = 'src/less/**/';
  gulp.watch(src + '*', function(event){
    var paths = watchPath(event , src , 'dist/css/');
    console.log(paths.srcFilename + ' has ' + event.type);
    if(paths.srcFilename.indexOf('.less')>-1 || paths.srcFilename.indexOf('.less')>-1){
      gulp.src('src/less/**/' + paths.srcFilename)
          .pipe(less())
          .on('error', handleErrors)
          .pipe(minifyCss())
          .pipe(gulp.dest('dist/css/'))
          .pipe(reload({stream: true}));
    }
  });
});

gulp.task('htmlMin', function() {
  var options = {
    removeComments: true,//清除HTML注释
    collapseWhitespace: true,//压缩HTML
    collapseBooleanAttributes: true,//省略布尔属性的值 <input checked="true"/> ==> <input />
    removeEmptyAttributes: true,//删除所有空格作属性值 <input id="" /> ==> <input />
    removeScriptTypeAttributes: true,//删除<script>的type="text/javascript"
    removeStyleLinkTypeAttributes: true,//删除<style>和<link>的type="text/css"
    minifyJS: true,//压缩页面JS
    minifyCSS: true//压缩页面CSS
  };
  var src = 'src/';
  gulp.watch([src + '*']).on('change', function(event){
    var paths = watchPath(event , src , 'dist/');
    console.log(paths.srcFilename + ' has ' + event.type);
    if(paths.srcFilename.indexOf('.html')>-1 || paths.srcFilename.indexOf('.html')>-1){
      gulp.src(src + paths.srcFilename)
          .pipe(htmlmin(options))
          .pipe(gulp.dest('dist/'));
          // .on('change', reload)
    }
  });
});
gulp.task('watchHtml', function() {
  gulp.watch([
    './dist/*.html']).on('change', reload);
});
gulp.task('watch', ['watchHtml','htmlMin','watchSass','watchJs'], function() {
    browserSync.init({
      proxy: "http://www.single.com/",
      port: 82
    });
});
